package com.nand.module.function.ui;

import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.Callback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.module.function.R;
import com.nand.module.function.adapter.UpkeepDetailsAdapter;
import com.nand.module.function.bean.IdBean;
import com.nand.module.function.bean.ManageBean;
import com.nand.module.function.bean.StandardListBean;
import com.nand.module.function.bean.UpkeepDetailsBean;
import com.nand.module.function.constant.URL;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.function.utils.ToolUtils;
import com.nand.module.function.utils.dateutils.ConvertUtils;
import com.nand.module.function.utils.dateutils.DatePicker;
import com.nand.module.function.utils.dateutils.DateTimePicker;
import com.nand.module.function.view.CommonPopupWindow;
import com.nand.module.function.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.internal.util.LogUtil;

public class UpkeepDetalisActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout llJbxx;
    private LinearLayout llBynr;
    private View view1;
    private View view2;
    private TextView mTvBynr;
    private TextView mTvJbxx;
    private RecyclerView mRecycler;
    private UpkeepDetailsAdapter mRecyclerAdapter;
    private int type;
    private TextView mTvSbbh;
    private TextView mTvSbmc;
    private TextView mTvSybm;
    private TextView mTvJhkssj;
    private TextView mTvJhjssj;
    private TextView mTvSjjssj;
    private TextView mTvSjkssj;
    private List<StandardListBean> mList = new ArrayList<>();
    private TextView mTvConfirm;
    private String equipBelong;
    private CommonPopupWindow window,windowContent;
    private WheelView mWheelview;
    private TextView mTvBynx;
    private ImageView mIvRightArrow2;
    private ImageView mIvRightArrow3;
    private int mtId;
    private String optimisticLockField;
    private int day;
    private int month;
    private int year;
    private int hour;
    private int minute;
    private TextView tvHeadline;
    private DateTimePicker picker,picker2;
    private TextView horiText;
    private CommonPopupWindow.LayoutGravity layoutGravity;
    private String maintenanceType;

    @Override
    protected int initLayout() {
        return R.layout.activity_upkeep_detalis;
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type", 1);
        mtId = getIntent().getIntExtra("mtId", 1);
        llJbxx = findViewById(R.id.ll_jbxx);
        llBynr = findViewById(R.id.ll_bynn);

        findViewById(R.id.btn_jbxx).setOnClickListener(this);
        findViewById(R.id.btn_bynr).setOnClickListener(this);
        //view1 = findViewById(R.id.view1);
       // view2 = findViewById(R.id.view2);
        mTvJbxx = findViewById(R.id.tv_jbxx);
        mTvBynr = findViewById(R.id.tv_bynr);
        mRecycler = findViewById(R.id.recyclerview);
        tvHeadline = findViewById(R.id.tv_headline);

        mTvSbbh = findViewById(R.id.tv_sbbh);
        mTvSbmc = findViewById(R.id.tv_sbmc);
        mTvSybm = findViewById(R.id.tv_sybm);
        mTvBynx = findViewById(R.id.tv_bylx);
        mTvJhkssj = findViewById(R.id.tv_jhkssj);
        mTvJhjssj = findViewById(R.id.tv_jhjssj);
        mTvSjkssj = findViewById(R.id.tv_sjkssj);
        mTvSjjssj = findViewById(R.id.tv_sjjssj);
        mTvConfirm = findViewById(R.id.tv_confirm);
        mTvConfirm.setOnClickListener(this);
        mIvRightArrow2 = findViewById(R.id.iv_right_arrow2);
        mIvRightArrow3 = findViewById(R.id.iv_right_arrow3);

        switch (type) {
            case 1:
                tvHeadline.setText("设备保养详情");
                mTvConfirm.setVisibility(View.GONE);
                mIvRightArrow2.setVisibility(View.GONE);
                mIvRightArrow3.setVisibility(View.GONE);
                break;
            case 2:
                tvHeadline.setText("设备保养待执行");
                findViewById(R.id.ll_sjkssj).setOnClickListener(this);
                findViewById(R.id.ll_sjjssj).setOnClickListener(this);
                break;
        }

        year = ToolUtils.getYear();
        month = ToolUtils.getMonth();
        day = ToolUtils.getDay();
        hour = ToolUtils.getHour();
        minute = ToolUtils.getMinute();
        iniktAddBean();
        initPop();
    }

    private void iniktAddBean() {
        IdBean idBean = new IdBean();
        idBean.setId(0);
        idBean.setType("电器保养");
        PLANETS.add(idBean);
        IdBean idBean1 = new IdBean();
        idBean1.setId(1);
        idBean1.setType("机械保养");
        PLANETS.add(idBean1);
    }

    @Override
    protected void initData() {
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerAdapter = new UpkeepDetailsAdapter(UpkeepDetalisActivity.this);
        mRecycler.setAdapter(mRecyclerAdapter);

        mRecyclerAdapter.setOnItemClickListener(new UpkeepDetailsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onJcnrClick(int position, String context, TextView textview) {
                layoutGravity=new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.ALIGN_LEFT| CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                layoutGravity.setHoriGravity(CommonPopupWindow.LayoutGravity.ALIGN_RIGHT);
                layoutGravity.setVertGravity(CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                windowContent.showBashOfAnchor(textview, layoutGravity, 0, 0);
                horiText.setText(context);
            }
        });

        HttpParams params1 = new HttpParams();
        params1.put("mtId", mtId);
        OkGoUtils.getInstance().postByOkGo(this, URL.MAINTENANCE_VIEW, params1, ResponseBean.class, callback1, 1);

    }

    private Callback callback1 = new Callback<ResponseBean>() {


        @Override
        public void onSuccess(ResponseBean response, int id) {
            if (response.getCode() == 200) {
                String data = response.getDataInfo().toString();
                UpkeepDetailsBean bean = JSON.parseObject(data, UpkeepDetailsBean.class);

                equipBelong = bean.getEquipBelong();
                mTvSbbh.setText(bean.getMaintenanceId());
                mTvSbmc.setText(bean.getEquipName());
                mTvSybm.setText(bean.getUseDepart());
                maintenanceType = bean.getMaintenanceType();
                mTvBynx.setText(maintenanceType);
                String planStartDate = bean.getPlanStartDate();
                String substring = planStartDate.substring(0, planStartDate.length() - 3);
                String planEndDate = bean.getPlanEndDate();
                String substring2 = planEndDate.substring(0, planEndDate.length() - 3);
                mTvJhkssj.setText(substring);
                mTvJhjssj.setText(substring2);
                switch (type) {
                    case 1:


                        String actualStartDate = bean.getActualStartDate();
                        String actualEndDate = bean.getActualEndDate();
                        if (actualStartDate.length() > 3 && actualEndDate.length() > 3){
                            String substrin = actualStartDate.substring(0, actualStartDate.length() - 3);
                            String substrin2 = actualEndDate.substring(0, actualEndDate.length() - 3);
                            mTvSjkssj.setText(substrin);
                            mTvSjjssj.setText(substrin2);
                        }else {
                            mTvSjkssj.setText(actualStartDate);
                            mTvSjjssj.setText(actualEndDate);
                        }


                        for (int i = 0; i < bean.getActualList().size(); i++) {

                            StandardListBean standardListBean = new StandardListBean();
                            standardListBean.setStandMainContent(bean.getActualList().get(i).getStandMainContent());
                            standardListBean.setMainContent(bean.getActualList().get(i).getMainContent());
                            standardListBean.setoKorNG(bean.getActualList().get(i).getOKorNG()+"");
                            mList.add(standardListBean);
                        }
                        mRecyclerAdapter.setData(mList, 1);
                        break;
                    case 2:

                        mTvSjkssj.setText("请选择");
                        mTvSjjssj.setText("请选择");

                        optimisticLockField = bean.getOptimisticLockField();
                        if ("".equals(optimisticLockField)){
                            optimisticLockField = "0";
                        }
                        for (int i = 0; i < bean.getStandardList().size(); i++) {

                            StandardListBean standardListBean = new StandardListBean();
                            standardListBean.setStandMainContent(bean.getStandardList().get(i).getStandMainContent());
                            standardListBean.setMainContent(bean.getStandardList().get(i).getMainContent());
                            mList.add(standardListBean);
                        }
                        mRecyclerAdapter.setData(mList, 2);
                        break;
                }


            } else {
                showToast(response.getMsg());
            }
        }

        @Override
        public void onError(Throwable e, int id) {

        }
    };

    private boolean isClick = false;
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_jbxx) {
            llJbxx.setVisibility(View.VISIBLE);
            llBynr.setVisibility(View.GONE);
            findViewById(R.id.btn_jbxx).setBackgroundColor(getResources().getColor(R.color.color_1a0077f7));
            findViewById(R.id.btn_bynr).setBackgroundColor(getResources().getColor(R.color.white));
            //findViewById(R.id.btn_jbxx).setVisibility(View.VISIBLE);
            //view2.setVisibility(View.INVISIBLE);
            mTvJbxx.setTextColor(getResources().getColor(R.color.color_33));
            mTvBynr.setTextColor(getResources().getColor(R.color.color_99));
        } else if (id == R.id.btn_bynr) {
            llBynr.setVisibility(View.VISIBLE);
            llJbxx.setVisibility(View.GONE);
            findViewById(R.id.btn_bynr).setBackgroundColor(getResources().getColor(R.color.color_1a0077f7));
            findViewById(R.id.btn_jbxx).setBackgroundColor(getResources().getColor(R.color.white));
           // view2.setVisibility(View.VISIBLE);
            //view1.setVisibility(View.INVISIBLE);
            mTvBynr.setTextColor(getResources().getColor(R.color.color_33));
            mTvJbxx.setTextColor(getResources().getColor(R.color.color_99));
        }  else if (id == R.id.ll_sjkssj) {
            picker = new DateTimePicker(this, DateTimePicker.HOUR_24);

            picker.setCanceledOnTouchOutside(true);
            picker.setUseWeight(true);
            picker.setTopPadding(ConvertUtils.toPx(this, 10));
            picker.setDateRangeStart(2000, 1, 1);
            picker.setTimeRangeStart(hour,day);
            picker.setDateRangeEnd(2060, 12, 31);
            picker.setSelectedItem(year, month, day,hour,minute);

            picker.setTimeRangeStart(0, 0);
            picker.setTimeRangeEnd(23, 59);
            picker.setResetWhileWheel(false);
            picker.setTopLineColor(getResources().getColor(R.color.color_33));
            picker.setLabelTextColor(getResources().getColor(R.color.color_33));
            picker.setDividerColor(getResources().getColor(R.color.color_33));
           // picker.setTopBackgroundColor(getResources().getColor(R.color.color_33));
            picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
                @Override
                public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                   // showToast(year + "-" + month + "-" + day + " " + hour + ":" + minute);
                    mTvSjkssj.setText(year + "-" + month + "-" + day+ " " + hour + ":" + minute);
                    isClick = true;
                    actualStartDate = year + "-" + month + "-" + day+ " " + hour + ":" + minute;
                }
            });

            picker.setOnWheelListener(new DateTimePicker.OnWheelListener() {
                @Override
                public void onYearWheeled(int index, String year) {
                    picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay()+ "-" + picker.getSelectedHour() + "-" + picker.getSelectedMinute());
                    UpkeepDetalisActivity.this.year = Integer.parseInt(year);
                }

                @Override
                public void onMonthWheeled(int index, String month) {
                   // picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
                    picker.setTitleText(picker.getSelectedYear() + "-" +month + "-" + picker.getSelectedDay()+ "-" + picker.getSelectedHour() + "-" + picker.getSelectedMinute());
                    UpkeepDetalisActivity.this.month = Integer.parseInt(month);
                }

                @Override
                public void onDayWheeled(int index, String day) {
                    //picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
                    picker.setTitleText(picker.getSelectedYear() + "-" +picker.getSelectedMonth() + "-" + day+ "-" + picker.getSelectedHour() + "-" + picker.getSelectedMinute());
                    UpkeepDetalisActivity.this.day = Integer.parseInt(day);
                }

                @Override
                public void onHourWheeled(int index, String hour) {
                    picker.setTitleText(picker.getSelectedYear() + "-" +picker.getSelectedMonth() + "-" +  picker.getSelectedDay()+ "-" + hour + "-" + picker.getSelectedMinute());
                    UpkeepDetalisActivity.this.hour = Integer.parseInt(hour);
                }

                @Override
                public void onMinuteWheeled(int index, String minute) {
                    picker.setTitleText(picker.getSelectedYear() + "-" +picker.getSelectedMonth() + "-" +  picker.getSelectedDay()+ "-" + picker.getSelectedHour() + "-" + minute);
                    UpkeepDetalisActivity.this.minute = Integer.parseInt(minute);
                }
            });
            picker.show();
        } else if (id == R.id.ll_sjjssj) {
            if (! isClick){
                showToast("请先选择实际开始时间");
                return;
            }
            picker2 = new DateTimePicker(this, DateTimePicker.HOUR_24);

            picker2.setCanceledOnTouchOutside(true);
            picker2.setUseWeight(true);
            picker2.setTopPadding(ConvertUtils.toPx(this, 10));
            // picker.setRangeEnd(2060, 12, 31);

            LogUtils.e("avvv========"+year);
            LogUtils.e("avvv========"+month);
            LogUtils.e("avvv========"+day);
            LogUtils.e("avvv========"+hour);
            LogUtils.e("avvv========"+minute);
            picker2.setDateRangeStart(year, month, day);
            picker2.setTimeRangeStart(hour,minute);
            picker.setSelectedItem(year, month, day,hour,minute);
            picker2.setDateRangeEnd(2060, 12, 31);
            picker2.setTimeRangeStart(hour, minute);
            picker2.setTimeRangeEnd(23, 59);
            picker2.setResetWhileWheel(false);
            picker2.setTopLineColor(getResources().getColor(R.color.color_33));
            picker2.setLabelTextColor(getResources().getColor(R.color.color_33));
            picker2.setDividerColor(getResources().getColor(R.color.color_33));
            // picker.setTopBackgroundColor(getResources().getColor(R.color.color_33));
            picker2.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
                @Override
                public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                    mTvSjjssj.setText(year + "-" + month + "-" + day+"  "+ hour + ":" + minute);
                    actualEndDate = year + "-" + month + "-" + day+ " " + hour + ":" + minute;
                }
            });

            picker2.setOnWheelListener(new DateTimePicker.OnWheelListener() {
                @Override
                public void onYearWheeled(int index, String year) {
                    picker2.setTitleText(year + "-" + picker2.getSelectedMonth() + "-" + picker2.getSelectedDay()+ "-" + picker2.getSelectedHour() + "-" + picker2.getSelectedMinute());
                 //   UpkeepDetalisActivity.this.year = Integer.parseInt(year);
                }

                @Override
                public void onMonthWheeled(int index, String month) {
                    // picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
                    picker2.setTitleText(picker2.getSelectedYear() + "-" +month + "-" + picker2.getSelectedDay()+"-" +  picker2.getSelectedHour() + "-" + picker2.getSelectedMinute());
                  //  UpkeepDetalisActivity.this.month = Integer.parseInt(month);
                }

                @Override
                public void onDayWheeled(int index, String day) {
                    //picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
                    picker2.setTitleText(picker2.getSelectedYear() + "-" +picker2.getSelectedMonth() + "-" + day+ "-" + picker2.getSelectedHour() + "-" + picker2.getSelectedMinute());
                //    UpkeepDetalisActivity.this.day = Integer.parseInt(day);
                }

                @Override
                public void onHourWheeled(int index, String hour) {
                    picker2.setTitleText(picker2.getSelectedYear() + "-" +picker2.getSelectedMonth() + "-" +  picker2.getSelectedDay()+ "-" + hour + "-" + picker2.getSelectedMinute());
                  //  UpkeepDetalisActivity.this.day = Integer.parseInt(hour);
                }

                @Override
                public void onMinuteWheeled(int index, String minute) {
                    picker2.setTitleText(picker2.getSelectedYear() + "-" +picker2.getSelectedMonth() + "-" +  picker2.getSelectedDay()+"-" +  picker2.getSelectedHour() + "-" + minute);
                  //  UpkeepDetalisActivity.this.day = Integer.parseInt(minute);
                }
            });
            picker2.show();



        } else if (id == R.id.tv_confirm) {
            List<StandardListBean> bean = mRecyclerAdapter.getStandardListBean();
            String s = JSON.toJSONString(bean);

            if ("".equals(actualStartDate)) {
                showToast("请选择实际开始时间");
                return;
            }
            if ("".equals(actualEndDate)) {
                showToast("请选择实际结束时间");
                return;
            }

            for (int i = 0; i < bean.size(); i++) {
                if ("" != bean.get(i).getoKorNG()) {
                    whether = 1;
                }
            }

            if (whether == 0){
                showToast("请选择保养内容 合/不合格");
                return;
            }

            HttpParams params2 = new HttpParams();
            params2.put("mtId", mtId);
            params2.put("equipBelong", equipBelong);
            params2.put("maintenanceType", maintenanceType);//电器 机械
            params2.put("actualStartDate", actualStartDate+":00");
            params2.put("actualEndDate", actualEndDate+":00");
            params2.put("mainContent", s);
            params2.put("optimisticLockField", optimisticLockField);


            LogUtils.e("av=======" + actualStartDate);
            LogUtils.e("av=======" + actualEndDate);
            LogUtils.e("av=======" + s);
            LogUtils.e("av=======" + optimisticLockField);
            LogUtils.e("av=======" + mtId);
            LogUtils.e("av=======" + item);
            LogUtils.e("av=======" + equipBelong);

         OkGoUtils.getInstance().postByOkGo(this, URL.MAINTENANCE_CARRIED, params2, ResponseBean.class, callback2, 2);
        }
    }
    private int whether = 0;

    private String actualStartDate = "";
    private String actualEndDate = "";

    private Callback callback2 = new Callback<ResponseBean>() {


        @Override
        public void onSuccess(ResponseBean response, int id) {
            if (response.getCode() == 200) {
                showToast("提交成功");
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
               // EventBus.getDefault().post(new ManageBean(""));
                finish();
            } else {
                showToast(response.getMsg());
            }
        }

        @Override
        public void onError(Throwable e, int id) {

        }
    };


    private String item = "";
    // private static final String[] PLANETS = new String[]{"电器保养", "机械 保养"};
    ArrayList PLANETS = new ArrayList<IdBean>();

    private void initPop() {
        windowContent = new CommonPopupWindow(this, R.layout.item_show_content, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
            @Override
            protected void initView() {
                View view=getContentView();
                horiText = view.findViewById(R.id.hori_text);
            }

            @Override
            protected void initEvent() {}
        };


        window = new CommonPopupWindow(this, R.layout.popup_item_select, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {


            private TextView mPopTvConfirm;
            private TextView mTvCancel;

            @Override
            protected void initView() {
                View view = getContentView();
                mWheelview = view.findViewById(R.id.wheelview);
                mPopTvConfirm = view.findViewById(R.id.tv_confirm);
                mTvCancel = view.findViewById(R.id.tv_cancel);
                mWheelview.setVisibility(View.VISIBLE);

                mWheelview.setOffset(1);
                mWheelview.setItems(PLANETS);
                mWheelview.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
                    @Override
                    public void onSelected(int selectedIndex, IdBean ite) {
                       // Log.d(TAG, "selectedIndex: " + selectedIndex + ", item_layout: " + item_layout.getType() +",id=="+item_layout.getId());
                        item = ite.getType();
                    }
                });
                mPopTvConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mTvBynx.setText(item);
                      //  mTvBynx.setTextColor(getResources().getColor(R.color.color_66));

                        window.getPopupWindow().dismiss();
                    }
                });

                mTvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        window.getPopupWindow().dismiss();
                    }
                });
            }

            @Override
            protected void initEvent() {
            }

            @Override
            protected void initWindow() {
                super.initWindow();
                PopupWindow instance = getPopupWindow();
                instance.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams lp = getWindow().getAttributes();
                        lp.alpha = 1.0f;
                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                        getWindow().setAttributes(lp);
                    }
                });
            }
        };
    }
}

